package com.io.buff;

import org.junit.Test;

import java.io.*;

public class Writer_ {
    public static void main(String[] args) {
        System.out.println("写的区别");
    }

    @Test
    public void fileOutputStream_() {
        // 输出流 字节操作
        String filePath = "E:\\test\\b.txt";
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(filePath);
            // 写入内容
            String str = "中国发展越来越好了！\r\n";
            fos.write(str.getBytes());
            String note = "加油小伙子，实现自己的目标";
            fos.write(note.getBytes());
            System.out.println("写入成功！");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源流
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void fileWrite_() {
        // 使用 字符流 fileWrite 写入测试
        String filePath = "E:\\test\\b.txt";
        FileWriter fw = null;
        char[] content = {'h','e','l','l','o'};
        try {
            fw = new FileWriter(filePath,true);
            // 写入文件内容
            String str = "\n不需要想太多，该来的总会来的\r\n";
            fw.write(str);
            String note = "努力慢慢该有的总会有的\r\n";
            fw.write(note);
            // 写入字符数组
            fw.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源流
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void bufferedOutputStream_() {
        // 写文件内容 字节操作
        String filePath = "E:\\test\\b.txt";
        BufferedOutputStream bis = null;
        try {
            bis = new BufferedOutputStream(new FileOutputStream(filePath, true));
            // 写入内容
            String str = "\r\n不负青春，不负韶华!\r\n";
            bis.write(str.getBytes());
            String note = "《以梦为马》";
            bis.write(note.getBytes());
            System.out.println("写入结束");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭外层资源流，底层自动关闭节点流
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
